package net.quindu.android.smsfilter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class FilterActionActivity extends Activity {
	public final static int SUCCESS_RETURN_CODE = 1;
	public final static int PICK_CONTACT = 300;
	
	SQLiteDatabase db;
	int filterId;
	int conditionId;
	boolean isNew = true;
	int filterConditionId;
	EditText editTextValue;
	Button contactsButton;
	Button deleteButton;
	Spinner typeSpinner;
	ArrayAdapter<MenuElement> typeList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		Bundle filterIdBundle = getIntent().getExtras();
		filterId = filterIdBundle.getInt("filter_id");
		
		conditionId = filterIdBundle.getInt("id");
		if(conditionId == 0){
			isNew = true;
		}else{
			isNew = false;
		}
		

		super.onCreate(savedInstanceState);
		setTitle("Action");
		setContentView(R.layout.condition);
		editTextValue = (EditText) findViewById(R.id.ValueEditText);
		
		
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        db = dbHelper.getReadableDatabase();			
		
		
		typeSpinner = (Spinner) findViewById(R.id.TypeSpinner);
		typeList = new ArrayAdapter<MenuElement>(this, android.R.layout.simple_list_item_1);
		
		if(isNew){
			typeList.add(new MenuElement( this.getString(R.string.select_action),0));
		}
		
		typeList.add(new MenuElement( this.getString(R.string.action_type_popup),5));
		typeList.add(new MenuElement( this.getString(R.string.action_type_delete_older),6));
		typeList.add(new MenuElement( this.getString(R.string.action_type_delete),7));
		
		
		typeList.add(new MenuElement( this.getString(R.string.action_type_ring),1));
		typeList.add(new MenuElement( this.getString(R.string.action_type_vibrate),2));
		typeList.add(new MenuElement( this.getString(R.string.action_type_ring_prevent),3));
		typeList.add(new MenuElement( this.getString(R.string.action_type_vibrate_prevent),4));
		
		
		typeSpinner.setAdapter(typeList);
	
		
		typeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	setType((int) typeList.getItem(position).getId());
		    }

		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {
		        // your code here
		    }

		});
		
		contactsButton = (Button) findViewById(R.id.ContactButton);
		contactsButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				  Intent intent = new Intent(Intent.ACTION_PICK);
				  intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
				  startActivityForResult(intent, PICK_CONTACT);
			}
		});
		
		deleteButton = (Button) findViewById(R.id.DeleteButton);
		deleteButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				  delete();
			}
		});		
		
		
		
		//- load old values
		if(!isNew){
			filterId = filterIdBundle.getInt("id");			
	        Cursor cursor = db.rawQuery("SELECT val, type_id FROM sms_action WHERE _id=" + conditionId + "", null);
	        cursor.moveToNext();
	        setType(cursor.getInt(1));	
	        editTextValue.setText(cursor.getString(0));
		}
		
	}
	
	public void setType(int type){
		contactsButton.setVisibility(View.GONE);
		editTextValue.setVisibility(View.GONE);
	}
	public void delete(){
		db.delete("sms_action", "_id = " + conditionId, null);
		finish();
	}
	
	public void saveValues(){
		int type = (int) typeList.getItem(typeSpinner.getSelectedItemPosition()).getId();
		String value = editTextValue.getText().toString();
		
		if(type == 0 && isNew){
			return;
		}
		
		ContentValues initialValues = new ContentValues();
		initialValues.put("val", value); 		  
		initialValues.put("type_id", type);
		initialValues.put("filter_id", filterId);
				  		
		
		if(isNew){
			db.insert("sms_action",null, initialValues);
		}else{
			db.update("sms_action", initialValues, "_id = " + conditionId, null);
		}
		
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);				 	
		
	}	
	
	@Override
	public void onBackPressed() {
		saveValues();
		super.onBackPressed();
	}	
	
	
	@Override
	public void onStop() {
          setResult(RESULT_OK, null);
		  super.onStop();
		}	
	
}